import { Injectable } from '@angular/core';
import { StateMachineService } from "@farris/command-services";
import { FrameContext } from '@farris/devkit';
import { BefRepository, ResponseInfo } from '@farris/bef';
import { FormMessageService, FormNotifyService, CommandService, FormLoadingService, BeActionService } from '@farris/command-services';
import { EMPTY, of } from 'rxjs';
import { tap, map, switchMap } from 'rxjs/operators';

@Injectable()
export class NewsbbypzFrmControllerService {
  constructor(
    private frameContext: FrameContext,
    private messageService: FormMessageService,
    private beActionService: BeActionService,
    public stateMachineService: StateMachineService,
    public formNotifyService: FormNotifyService,
    private commandService: CommandService,
    public loadingService: FormLoadingService
  ) { }


  /**
* 加载二保数据
* @remarks 
* @returns 
*/
  loadchild2(): any {
    this.commandService.execute("datagridLoadSubList2")
  }

  /**
   * 删除关联1
   * @remarks 
   * @returns 
   */
  deletechange1(): any {
    const data = this.frameContext.bindingData;
    const mainid = data.getValue(['id']);
    const checkeds = this.frameContext.appContext.componentManager.get(['bybz-3h7x-component', 'bybz-3h7x-dataGrid']).checkeds;
    let ids: string[] = [];
    for (let item of checkeds) {
      ids.push(item.id);
    }
    debugger
    const repository = this.frameContext.repository as BefRepository<any>;
    const requestInfo = repository.restService.buildRequestInfo();
    const actionUri = 'deletechange';
    const body = {
      mainid: mainid,
      ids: ids,
      lx: '1',
      requestInfo: requestInfo
    }
    const action$ = this.beActionService.invokeAction(actionUri, 'PUT', null, null, body);
    return action$.pipe(
      tap((responseInfo: ResponseInfo) => {
        this.commandService.execute("Load1")
      })
    )
  }

  /**
 * 删除关联2
 * @remarks 
 * @returns 
 */
  deletechange2(): any {
    const data = this.frameContext.bindingData;
    const mainid = data.getValue(['id']);
    const checkeds = this.frameContext.appContext.componentManager.get(['ebbz-pznx-component', 'ebbz-pznx-dataGrid']).checkeds;
    let ids: string[] = [];
    for (let item of checkeds) {
      ids.push(item.id);
    }
    debugger
    const repository = this.frameContext.repository as BefRepository<any>;
    const requestInfo = repository.restService.buildRequestInfo();
    const actionUri = 'deletechange';
    const body = {
      mainid: mainid,
      ids: ids,
      lx: '2',
      requestInfo: requestInfo
    }
    const action$ = this.beActionService.invokeAction(actionUri, 'PUT', null, null, body);
    return action$.pipe(
      tap((responseInfo: ResponseInfo) => {
        this.commandService.execute("Load1")
      })
    )
  }
}